import {cbs} from '../../config/settings.js';
import {mapGetters} from 'vuex';
import Upload from '../Common/Upload/';

module.exports = {
	data() {
		let notEmpty = (rule, value, cb) => {
				if(!value) {
					cb(new Error('请填写此项'));
				}else {
					cb();
				}
			}
		return {
			valiForm: {
				companyName: '',                // 营业执照公司名
				address: '',                    // 营业执照地址
				licenceImg: '',                 // 营业执照照片
				name: '',                       // 申请人姓名
				proposerType: 0,                // 申请人身份
				cardId: '',                     // 申请人身份证号码
				proposerFrontImg: '',           // 申请人身份证正面照
				proposerBackImg: '',            // 申请人身份证反面照
				unProposerImgUrl: '',           // 法人委托书
			},
			valiRules: {
				companyName: [{
					validator: notEmpty,
					trigger: 'blur'
				}],
				address: [{
					validator: notEmpty,
					trigger: 'blur'
				}],
				licenceImg: [{
					validator: notEmpty,
					trigger: 'blur'
				}],
				name: [{
					validator: notEmpty,
					trigger: 'blur'
				}],
				proposerType: [{
					validator: notEmpty,
					trigger: 'blur'
				}],
				cardId: [{
					validator: (rule, value, cb) => {
						if(!value) {
							cb(new Error('请填写此项'));
						}else if(!/^\d{17}[\d|x|X]$/.test(value)) {
							cb(new Error('请填写正确的身份证号码'));
						}else {
							cb();
						}
					},
					trigger: 'blur'
				}],
				proposerFrontImg: [{
					validator: notEmpty,
					trigger: 'blur'
				}],
				proposerBackImg: [{
					validator: notEmpty,
					trigger: 'blur'
				}],
				unProposerImgUrl: [{
					validator: (rule, value, cb) => {
						if(this.valiForm.proposerType === 1) {
							if(!value) {
								cb(new Error('请上传法人委托书'));
							}
						}else {
							cb();
						}
					}
				}]
			},
			// dialog
			isAuthenDialogVisible: false,
			isTipDialogVisible: false,
			tipDialogText: ''
		}
	},
	components: {
		Upload
	},
	methods: {
		onGetLicenceUrl(value) {
			this.valiForm.licenceImg = value;
		},
		onGetProposerFrontUrl(value) {
			this.valiForm.proposerFrontImg = value;
		},
		onGetProposerBackUrl(value) {
			this.valiForm.proposerBackImg = value;
		},
		onGetUnProposerUrl(value) {
			this.valiForm.unProposerImgUrl = value;
		},
		onRegister(name) {
			this.$refs[name].validate(valid => {
				if(valid) {
					if(this.valiForm.proposerType === 0) {
						this.valiForm.unProposerImgUrl = '';
						this.$refs.child.changeImgUrl();
					}

					let data = this.$$formData({
						companyName: this.valiForm.companyName,
						companyAddress: this.valiForm.address,
						businessLicense: this.valiForm.licenceImg,
						name: this.valiForm.name,
						applicantType: this.valiForm.proposerType,
						idCardNo: this.valiForm.cardId,
						idCardFrontUrl: this.valiForm.proposerFrontImg,
						idCardBackUrl: this.valiForm.proposerBackImg,
						powerOfAttorney: this.valiForm.unProposerImgUrl
					});

					this.$$api_authen_authenInfo(data, res => {
						if(res.event === 0) {
							this.onShowTipDialog('认证信息提交成功，正在审核中')
						}else {
							cbs.submitError.call(this, `提交失败，${res.msg}`);
						}
					})
				}
			})
		},
		onReset(name) {
			this.$refs[name].resetFields()
		},
		confirmLogout(fn=() => {}, errFn=() => {}) {
			this.$confirm('你确定退出登录么?', '确认退出', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(fn)
              .catch(errFn)
		},
		onDirLogout() {
			this.$$api_user_logout(null, res => {
				if(res.event === 0) {
	                this.$store.dispatch('remove_userinfo').then(() => {
	                    this.$router.push('/login');
						cbs.submitSuccess.call(this, '退出成功');
	                });
				}else {
					cbs.submitError.call(this, `退出失败，${res.msg}`);
				}
			})
		},
		onLogout() {
			this.confirmLogout(this.onDirLogout)
		},
		onShowTipDialog(text) {
			this.tipDialogText = text;
			this.isTipDialogVisible = true;
		},
		/**
         * 获取不在线时的消息
         */
        onGetMessages() {
            var a = [{
                title: 1,
                message: 'sdf'
            }, {
                title: 2,
                message: 'sdf'
            }]

            this.$$api_user_getMessage(null, res => {
                if(res.event === 0) {
                    this.showMessage(res.obj, 0, res.obj.length);
                }
            })
        },
        /**
         * 展示不在线时的消息
         */
        showMessage(arr, i, len) {
            if(i < len) {
                this.$alert(arr[i].message, arr[i].title, {
                    confirmButtonText: '确定',
                    callback: action => {
                        this.showMessage(arr, ++i, len)
                    }
                })
            }
        },
        /**
         * 退出登录
         */
        logout() {
        	this.$confirm('你确定退出登录么?', '确认退出', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
            	this.$$api_user_logout(null, res => {
	                if(res.event === 0) {
	                    this.$store.dispatch('remove_userinfo').then(() => {
	                        this.$router.push('/login');
	                    });
	                }else {
	                    cbs.submitError.call(this, `退出失败`);
	                }
	            })
            });
        },
	},
	computed: {
		dialogText() {
			let text = '';
			if(this.status === 1) {
				text = '您还未提价认证信息，请填写！'
			}else if(this.status === 3) {
				text = '您的认证信息审核不通过，请重新填写！'
			}

			return text;
		},
		...mapGetters({
			token: 'getToken',
			waterId: 'getUserId',
			status: 'getStatus'
		})
	},
	created() {
		if(this.status === 1 ||this.status === 3) {
			this.isAuthenDialogVisible = true;
		}else if(this.status === 2) {
			this.onShowTipDialog('您的资料在审核中，请耐心等候！');
		}

        this.onGetMessages();
	},
  	mounted() {
  	}
}